package com.itboye.zhujia.login;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.zhujia.R;
import com.itboye.zhujia.base.BaseActivity;
import com.itboye.zhujia.bean.PersonDataBean;
import com.itboye.zhujia.constants.SPContants;
import com.itboye.zhujia.controller.LoginController;
import com.itboye.zhujia.controller.LoginedState;
import com.itboye.zhujia.network.HttpRequest;
import com.itboye.zhujia.util.DebugLog;
import com.itboye.zhujia.util.SPUtils;
import com.itboye.zhujia.util.T;
import com.itboye.zhujia.volley.CodeErrorException;
import com.itboye.zhujia.volley.MyJsonRequest;
import com.itboye.zhujia.volley.XErrorListener;
import com.itboye.zhujia.volley.XRequestListener;
/**
 * 注册第3步（设置密码）
 *
 */
public class RegisterActivityThree extends BaseActivity implements OnClickListener{
	private ImageView close_icon;//返回图片
	private TextView add_shap_title_tv;//标题
	
	private EditText res_three_num;//密码
	private TextView res_three_ok;//完成并登陆
	private Intent intent;
	
	private String phone;//接上页面传来的手机号
	@Override
	public int initView() {
		return 0;
	}

	@Override
	public void onMyCreate() {
		setContentView(R.layout.activity_register_three);
		
		add_shap_title_tv.setText("设置密码");
		
		intent = super.getIntent();
		phone = intent.getStringExtra("phone");

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			
			break;
		case R.id.res_three_ok:
			UserRegister();
			break;

		default:
			break;
		}
		
	}
	/**
	 * 注册
	 */
	private void UserRegister() {
		if (res_three_num.getText().toString().trim() == null) {
			T.showLong(RegisterActivityThree.this, "请输入验证码");
			return;
		}
		MyJsonRequest<String> request = new MyJsonRequest.Builder<String>()
				.apiVer("100").typeKey("BY_User_register")
				.param("from", "0")
				.param("reg_type", "3")
				.param("username", phone)
				.param("password", res_three_num.getText().toString())
				.requestListener(new XRequestListener<String>() {

					@Override
					public void onResponse(String response) {
						// TODO Auto-generated method stub
						DebugLog.v("result", response.toString());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.USER_ID, response);
						SPUtils.put(RegisterActivityThree.this, null, SPContants.USERNAME,phone);
								
						SPUtils.put(RegisterActivityThree.this, null, SPContants.MOBILE, phone);
						// SPUtils.put(App.ctx, null, SPContants.PASSWORD,
						// passwordedit.getText().toString());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.IS_LOGINED, true);

						login();
						
						RegisterActivityThree.this.finish();

						// 发送广播通知LoginActivity结束
						Intent intent1 = new Intent(LoginActivity.LOGINEXIT);
						sendBroadcast(intent1);
						LoginController.setLoginState(new LoginedState());

					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							T.showLong(RegisterActivityThree.this, msg);
						} else {
							T.showLong(RegisterActivityThree.this, exception.toString());
						}

					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
	/**
	 * 登录
	 */
	public void login() {
		MyJsonRequest<PersonDataBean> request = new MyJsonRequest.Builder<PersonDataBean>()
				.apiVer("100")
				.typeKey("BY_User_login")
				.param("username",phone)
				.param("password",res_three_num.getText().toString())
				.requestListener(new XRequestListener<PersonDataBean>() {

					@Override
					public void onResponse(PersonDataBean response) {
						DebugLog.v("result", response.toString());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.WX_Openid,
								response.getWxopenid());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.USERNAME,
								response.getUsername());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.MOBILE,
								response.getMobile());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.IS_LOGINED, true);
						SPUtils.put(RegisterActivityThree.this, null, SPContants.EMAIL,
								response.getEmail());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.NICK,
								response.getNickname());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.PASSWORD,
								response.getPassword());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.USER_ID,
								response.getUid());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.AVATAR_URL,
								response.getHead());
						SPUtils.put(RegisterActivityThree.this, null, SPContants.AQ_NUM,
								response.getAqCount());
						RegisterActivityThree.this.finish();
						LoginController.setLoginState(new LoginedState());
						// 发送广播通知更改头像(还未实现)
						Intent intent2 = new Intent("changeavart");
						sendBroadcast(intent2);
					}
				}).errorListener(new XErrorListener() {

					@Override
					public void onErrorResponse(Exception exception, int code,
							String msg) {
						if (exception instanceof CodeErrorException) {
							T.showShort(RegisterActivityThree.this, msg);
						} else {
							T.showShort(RegisterActivityThree.this, exception.toString());
						}
					}
				}).build();
		HttpRequest.getDefaultRequestQueue().add(request);
	}
}
